package com.example.login.adapter;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.login.R;
import com.example.login.bean.NewsBean;

import java.util.ArrayList;
import java.util.List;

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {

    private List<NewsBean> data;

    public NewsAdapter(){

        data = new ArrayList<>();

        NewsBean apple = new NewsBean("如何看待失足少男深夜使用花呗消费高额化妆品，购买名牌服饰这一社会现象",R.drawable.huabei,"Vsinger官方","老V","124479");
        data.add(apple);
        NewsBean banana = new NewsBean("日本某绿发四字女明星疑似被曝私下与粉丝约会，并消费大量菜市场的葱",R.drawable.mikudate,"初音未来_Crypton","初音未来","4897465");
        data.add(banana);
        NewsBean orange = new NewsBean("我国著名红色歌姬登场2021年中央电视台春节联欢晚会并且一直在天上飞",R.drawable.tianyifei,"Vsinger官方","洛天依","988888");
        data.add(orange);
        NewsBean pear = new NewsBean("日前，海鲜市场上的章鱼不断涨价，涨价原因暂时不明，可能与某粉发女星有关",R.drawable.luka,"初音未来_Crypton","巡音流歌","3467845");
        data.add(pear);
        NewsBean banana1 = new NewsBean("根据Vsinger动物园的饲养员口述，近期园内时常发生竹子被盗事件，监控录像显示案犯为黑白相间发色",R.drawable.longya,"Vsinger官方","乐正龙牙","7849");
        data.add(banana1);
        NewsBean orange2 = new NewsBean("Piapro Character出版社的编辑表示，某15岁男明星与某K姓男歌手疑似在北海道同泡温",R.drawable.kaitolen,"初音未来_Crypton","镜音连","683473");
        data.add(orange2);
        NewsBean pear1 = new NewsBean("Piapro Character出版社的编辑表示，某15岁男明星与某K姓男歌手疑似在北海道同泡温",R.drawable.kaitolen,"初音未来_Crypton","镜音连","683473");
        data.add(pear1);
        NewsBean banana2 = new NewsBean("Piapro Character出版社的编辑表示，某15岁男明星与某K姓男歌手疑似在北海道同泡温",R.drawable.kaitolen,"初音未来_Crypton","镜音连","683473");
        data.add(banana2);
        NewsBean orange1 = new NewsBean("Piapro Character出版社的编辑表示，某15岁男明星与某K姓男歌手疑似在北海道同泡温",R.drawable.kaitolen,"初音未来_Crypton","镜音连","683473");
        data.add(orange1);

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater
                .from(parent.getContext())
                .inflate(R.layout.news_item,parent,false);//两种方法是等效的
//                view = View.inflate(parent.getContext(), R.layout.news_item, parent);//创建布局
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        NewsBean news = data.get(position);
        holder.article_title.setText(news.getTitletext());
        holder.article_user.setText(news.getUser());
        holder.tv_tag.setText(news.getTag());
        holder.tv_viewnumber.setText(news.getViewnumber());
        holder.img_cover.setImageResource(news.getImageView());
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        TextView article_title;
        TextView tv_tag;
        TextView article_user;
        TextView tv_viewnumber;
        ImageView img_cover;

        public ViewHolder(@NonNull View itemView){
            super(itemView);
            article_title = (TextView)itemView.findViewById(R.id.article_title);
            article_user = (TextView)itemView.findViewById(R.id.article_user);
            tv_tag = (TextView)itemView.findViewById(R.id.tv_tag);
            tv_viewnumber = (TextView)itemView.findViewById(R.id.tv_view_number);
            img_cover = (ImageView)itemView.findViewById(R.id.img_cover);
        }
    }
}
